package com.test;

import com.connection.DataConnection;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;

public class GuestActivity extends ListActivity {
	
	static String proPicture, proName, proPrice, proIntroduction;
	
	DataConnection data = new DataConnection(this);
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		data.open();
		Cursor c = data.queryProductInfo();
		String[] from = {"picture", "productName", "price"};
		int[] to = { R.id.image, R.id.text, R.id.price};
		
		SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,	R.layout.product, c, from, to);

		ListView listView = getListView();
		listView.setAdapter(adapter);
		
		listView.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				int id = (int) arg3 ;
				Toast.makeText(GuestActivity.this.getApplicationContext(), arg3 + "hello" + proPicture+"  "+proName+"  "+proPrice+"  "+proIntroduction, 2000).show();
				
				String tmpPicture[] = { "picture" };
				String tmpName[] = {"productName"};
				String tmpPrice[] = {"price"};
				String tmpIntroduction[] = {"introduction"};

				// TODO Auto-generated method stub
				Cursor cursor1 = data.db.query("Product", tmpPicture, "_id=?", new String[] { id + "" }, null, null, null);
				cursor1.moveToFirst();
				while (!cursor1.isAfterLast()) {
					for (int i = 0; i < cursor1.getColumnCount(); i++) {
						proPicture = cursor1.getString(i);

					}
					cursor1.moveToNext();
				}
				cursor1.close();
				
				Cursor cursor2 = data.db.query("Product", tmpName, "_id=?", new String[]{id+""}, null, null, null);
				cursor2.moveToFirst();
				while (!cursor2.isAfterLast()) {
					for (int i = 0; i < cursor2.getColumnCount(); i++) {
						proName = cursor2.getString(i);

					}
					cursor2.moveToNext();
				}
				cursor2.close();
				
				Cursor cursor3 = data.db.query("Product", tmpPrice, "_id=?", new String[]{id+""}, null, null, null);
				cursor3.moveToFirst();
				while (!cursor3.isAfterLast()) {
					for (int i = 0; i < cursor3.getColumnCount(); i++) {
						proPrice = cursor3.getString(i);

					}
					cursor3.moveToNext();
				}
				cursor3.close();
				
				Cursor cursor4 = data.db.query("Product", tmpIntroduction, "_id=?", new String[]{id+""}, null, null, null);
				cursor4.moveToFirst();
				while (!cursor4.isAfterLast()) {
					for (int i = 0; i < cursor4.getColumnCount(); i++) {
						proIntroduction = cursor4.getString(i);

					}
					cursor4.moveToNext();
				}
				cursor4.close();
			
				startActivity(new Intent(GuestActivity.this,
						GuestCheck.class));
			}

		});

	}

}
